<?php

namespace Juheip\IpService\platforms\jinyou;

use Juheip\IpService\contract\AbstractPlatform;

/**
 * 金柚网络开放平台 SDK
 * 通信编码协定 UTF-8
 */
class JinYouPlatform extends AbstractPlatform
{
    private $config;

    public function init(array $config): self
    {
        $this->config = $config;

        return $this;
    }

    /**
     * 通用时间戳10位
     */
    private  function timestamp()
    {
        return time();
    }
    
    /**
     * key排序签名
     */
    private function signAct($body)
    {
        $authStr         = "";
        $body ["st"]     = $this->timestamp();
        $body ["app_id"] = $this->config ['app_id'];

        ksort($body);

        foreach ($body as $item) {
            if(!is_object($item)) {
                $authStr = $authStr . $item;
            }
        }

        $body ["sign"] = md5(md5($authStr). $this->config ['app_key']);

        return $body;
    }

    /**
     * 测试接口,测试通信
     */
    public function apiTest($data)
    {
        return $this->client->request('post', $this->config ['server_url'] . 'api-test', $this->signAct($data));
    }

    /**
     * * 创建平台第三方接入账号
     * @param $phone        *接入方平台用户注册手机号 同应用下手机号码不可重复
     * @param $cn_name      *用户实名认证后的真实姓名
     * @param $cn_id        *用户实名认证后的身份证号码
     * @param $t_id         *接入方平台中客户唯一的身份标识
     * @return @json
     */
    public function createUsers($phone, $cn_name, $cn_id, $t_id)
    {
        $post_data = [
            'phone'   => $phone,
            'cn_name' => $cn_name,
            'cn_id'   => $cn_id,
            't_id'    => $t_id,
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'create-third-user', $this->signAct($post_data));
    }

    /**
     * 获取当前账户的可用余额
     * @return @json
     */
    public function getMyPoint()
    {
        return $this->client->request('post', $this->config ['server_url'] . 'get-my-point', $this->signAct([]));
    }

    /**
     * 检查VPN账户是否存在
     * @param string $user vpn账户
     * @param int $type 类型,1:静态;2:专线;
     */
    public function checkVpnAccountExists($user, $type)
    {
        $data = [
            'user' => $user,
            'type' => $type
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'check-vpn-account-exists', $this->signAct($data));
    }

    /**
     * 踢线
     * @param string $user VPN账户
     * @param int $type 类型,1:静态;2:专线;
     */
    public function kickOffLine($user, $type)
    {
        $data = [
            'user' => $user,
            'type' => $type
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'kick-off-line', $this->signAct($data));
    }

    /**
     * 上传用户手持身份证照片
     * 文件大小限制为不超过3M,支持格式为:jpg,jpeg,bmp,webp,png
     * @param $t_id mixed 接入方平台中客户唯一的身份标识
     * @param $upload_img mixed 这里是以thinkphp为示例，实例的ThinkFile($file_path)对象
     * 在CURLFILE(string $filename, string|null $mime_type = '', string|null $posted_filename = '')中你可以直接使用绝对地址,请勿使用网络地址。
     * @return @json
     */
    public function uploadExamineFile($t_id, $upload_img)
    {
        $post_data = array(
            't_id' => $t_id,
            'upload_img' => new \CURLFile($upload_img->getRealPath(), $upload_img->getMime(), 'up.jpg'),
        );

        return $this->client->request('post', $this->config ['server_url'] . 'upload-examine-file', $this->signAct($post_data));
    }

    /**
     * * 查询第三方接入用户状态
     * @param $t_id 接入方平台中客户唯一的身份标识，整型 数字范围1-999999999
     * @return @json
     */
    public function examineStatus($t_id)
    {
        $post_data = [
            't_id' => $t_id,
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'examine-status', $this->signAct($post_data));
    }

    /**
     * 开启白名单，涉及到用户需要使用到娱乐应用软件，例如：微信，抖音，均需要开启白名单
     * @param $user string vpn账户
     * @param $type int 账户类型,1:静态账户;2:专线账户
     * @param $t_id int 接入方平台中客户唯一的身份标识，整型 数字范围1-999999999
     * @return @json
     */
    public function setFirewallStatus($user, $type, $t_id)
    {
        $post_data = [
            'user' => $user,
            'type' => $type,
            't_id' => $t_id
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'set-firewall-status', $this->signAct($post_data));
    }

    /**
     * * 获取VPN账户列表
     * @param $page int 当前分页
     * @param $page_size int 分页条目数 1-100
     * @param $t_id int 接入方客户唯一值UID用于查询客户账号列表、0或缺省则查询当前应用下全部客户账号，非必填字段
     * @param $user string vpn账户
     * @return @json
     */
    public function userList($type, $page, $page_size, $t_id = null, $user = "")
    {
        $post_data = [
            'type'       => $type,
            't_id'       => $t_id,
            'user'       => $user,
            'page'       => $page,
            'page_size' => $page_size,
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'user-list', $this->signAct($post_data));
    }

    /**
     * 拉取已对接的产品ID
     * @param $type int 产品分类 1:静态;2:专线 非必填
     * @param $is_unshar int 产品类型 0:游戏独享;2:线路独享 非必填
     */
    public function productList($type = 1, $is_unshar = 0)
    {
        $post_data = [
            'type' => $type,
            'is_unshar' => $is_unshar
        ];

        $post_data = [];

        return $this->client->request('post', $this->config ['server_url'] . 'product-list', $this->signAct($post_data));
    }

    /**
     * 游戏列表
     */
    public function gameList()
    {
        $post_data = [
            
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'game-list', $this->signAct($post_data));
    }

    /**
     * 获取所有动态服务器列表
     * @param $area string 区域查询，可为空,例如："福建"
     * @return @json
     */
    public function getStaticNodeList($area = '')
    {
        $post_data = [
            'area' => $area
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'static-node-list', $this->signAct($post_data));
    }

    /**
     * 创建静态VPN账户
     * @param int $days 时长，通过已对接产品接口获取
     * @param int $game_id 游戏ID，通过游戏列表接口获取，线路独享时可以传空
     * @param string $account 自定义VPN账户,当存在自定义账户时且购买数量大于，则账户末尾会追加数量序号，例如账号为abc，数量为3，则会生成abc1-3
     * @param string $passwd 自定义VPN密码
     * @param string $nums 购买数量
     * @param int $t_id 第三方身份唯一标识符号
     * @param int $product_id 产品大类ID，通过已对接产品接口获取
     * @param int $product_sub_id 产品小类ID，通过已对接产品接口获取
     * @param int $product_item_id 产品ID，通过已对接产品接口获取
     */
    public function createStatic($days, $game_id, $account, $passwd, $nums, $t_id, $product_id, $product_sub_id, $product_item_id)
    {
        $post_data = [
            'days'            => $days,
            'product_item_id' => $product_item_id,
            'product_id'      => $product_id,
            'product_sub_id'  => $product_sub_id,
            'nums'            => $nums,
            't_id'            => $t_id,
            'game_id'         => $game_id,
            'account'         => $account,
            'passwd'          => $passwd
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'create-vpn-user', $this->signAct($post_data));
    }

    /**
     * 创建专线VPN账户
     * @param int $days 时长，通过已对接产品接口获取
     * @param int $game_id 游戏ID，通过游戏列表接口获取，线路独享时可以传空
     * @param string $account 自定义VPN账户,当存在自定义账户时且购买数量大于，则账户末尾会追加数量序号，例如账号为abc，数量为3，则会生成abc1-3
     * @param string $passwd 自定义VPN密码
     * @param string $nums 购买数量
     * @param int $t_id 第三方身份唯一标识符号
     * @param int $product_id 产品大类ID，通过已对接产品接口获取
     * @param int $product_sub_id 产品小类ID，通过已对接产品接口获取
     * @param int $product_item_id 产品ID，通过已对接产品接口获取
     */
    public function createFixed($days, $game_id, $account, $passwd, $nums, $t_id, $product_id, $product_sub_id, $product_item_id)
    {
        $post_data = [
            'days'            => $days,
            'product_item_id' => $product_item_id,
            'product_id'      => $product_id,
            'product_sub_id'  => $product_sub_id,
            'nums'            => $nums,
            't_id'            => $t_id,
            'game_id'         => $game_id,
            'account'         => $account,
            'passwd'          => $passwd
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'create-vpn-user', $this->signAct($post_data));
    }

    /**
     * 静态账户续费
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $account 要续费的VPN账户
     * @param int $product_id 产品大类ID，通过已对接产品接口获取
     * @param int $product_sub_id 产品小类ID，通过已对接产品接口获取
     * @param int $product_item_id 产品ID，通过已对接产品接口获取
     * @param int $days 时长，通过已对接产品接口获取
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function staticAccountRenew($t_id, $account, $product_id, $product_sub_id, $product_item_id, $days, $type = 0)
    {
        $post_data = [
            'days'            => $days,
            'product_item_id' => $product_item_id,
            'product_id'      => $product_id,
            'product_sub_id'  => $product_sub_id,
            't_id'            => $t_id,
            'account'         => $account,
            'type'            => $type
        ];

        return $this->client->request('post', $this->config ['server_url'] . 'account-renew', $this->signAct($post_data));
    }

    /**
     * 专线账户续费
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $account 要续费的VPN账户
     * @param int $product_id 产品大类ID，通过已对接产品接口获取
     * @param int $product_sub_id 产品小类ID，通过已对接产品接口获取
     * @param int $product_item_id 产品ID，通过已对接产品接口获取
     * @param int $days 时长，通过已对接产品接口获取
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function fixedAccountRenew($t_id, $account, $product_id, $product_sub_id, $product_item_id, $days, $type = 1)
    {
        $post_data = [
            'days'            => $days,
            'product_item_id' => $product_item_id,
            'product_id'      => $product_id,
            'product_sub_id'  => $product_sub_id,
            't_id'            => $t_id,
            'account'         => $account,
            'type'            => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'account-renew', $this->signAct($post_data));
    }
    

    /**
     * 获取地区剩余线路
     * @param int $product_id 产品ID
     * @param int $is_unshar 游戏独享0,线路独享1
     * @param int $game_id 游戏ID
     */
    public function regionSurplusOuts($product_id, $is_unshar, $game_id)
    {
        $post_data = [
            'product_id' => $product_id,
            'is_unshar'  => $is_unshar,
            'game_id'    => $game_id
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'region-surplus-outs', $this->signAct($post_data));
    }

    /**
     * 切换地区
     * @param int $t_id 第三方身份唯一标识符号
     * @param int $province_id 区域ID
     * @param int $rid 地区编码
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function changeRegion($t_id, $province_id, $rid, $account, $type = 0)
    {
        $post_data = [
            't_id'        => $t_id,
            'province_id' => $province_id,
            'rid'         => $rid,
            'account'     => $account,
            'type'        => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'change-region', $this->signAct($post_data));
    }

    /**
     * 批量切换地区
     * @param int $province 区域编码
     * @param int $rid ros编码
     * @param array $accounts VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function multiChangeRegion($province, $rid, $account, $type)
    {
        $post_data = [
            'province' => $province,
            'rid'      => $rid,
            'accounts' => $account,
            'type'     => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'multi-change-region', $this->signAct($post_data));
    }

    /**
     * 修改备注
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $remark 备注
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function editRemark($t_id, $remark, $account, $type = 0)
    {
        $post_data = [
            't_id'        => $t_id,
            'remark'      => $remark,
            'account'     => $account,
            'type'        => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'edit-remark', $this->signAct($post_data));
    }

    /**
     * 切换地区
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $new_password 新密码
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function editPassword($t_id, $new_password, $account, $type = 0)
    {
        $post_data = [
            't_id'        => $t_id,
            'passwd'      => $new_password,
            'account'     => $account,
            'type'        => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'edit-password', $this->signAct($post_data));
    }

    /**
     * 账户详情
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function accountInfo($t_id, $account, $type = 0)
    {
        $post_data = [
            't_id'        => $t_id,
            'account'     => $account,
            'type'        => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'account-info', $this->signAct($post_data));
    }

    /**
     * 静态地区列表
     * @param string $search_area 地名
     */
    public function regionNodes($search_area)
    {
        $post_data = [
            'search_area' => $search_area
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'region-nodes', $this->signAct($post_data));
    }

    /**
     * 静态账户在线列表
     */
    public function getStaticOnline($account, $page = 1, $page_size = 20)
    {
        $post_data = [
            'account'   => $account,
            'page'      => $page,
            'page_size' => $page_size
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'static-online', $this->signAct($post_data));
    }

    /**
     * VPN账户批量续费
     * @param array $accounts
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function multipleRenew($accounts, $type = 0)
    {
        $post_data = [
            'accounts' => $accounts,
            'type'     => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'multiple-renew', $this->signAct($post_data));
    }
    
    /**
     * 账户在线状态
     * @param string $account VPN账户 多账户以英文状态下逗号连接，例如:account1,account2
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function accountIsOnline($account, $type = 0)
    {
        $post_data = [
            'account'     => $account,
            'type'        => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'account-is-online', $this->signAct($post_data));
    }

    /**
     * 获取可切换IP列表
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function switchIpList($t_id, $account, $type = 0)
    {
        $post_data = [
            'account'     => $account,
            'type'        => $type,
            't_id'        => $t_id
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'switch-ip-list', $this->signAct($post_data));
    }

    /**
     * 保存切换的IP
     * @param int $t_id 第三方身份唯一标识符号
     * @param string $account VPN账户
     * @param $type int 账户类型,0:静态账户;1:专线账户
     */
    public function saveSwitchIp($t_id, $account, $type, $ip_id)
    {
        $post_data = [
            'account'     => $account,
            'type'        => $type,
            't_id'        => $t_id,
            'ip_id'       => $ip_id
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'save-switch-ip', $this->signAct($post_data));
    }

    /**
     * 批量续费
     * @param array $data 要续费的数据
     */
    public function multiRenew($data)
    {
        $post_data = [
            'data' => $data
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'multi-renew', $this->signAct($post_data));
    }

    /**
     * 退款接口
     * @param array $order_item_ids 退款子订单ID集合
     */
    public function refund($order_item_ids = [])
    {
        $post_data = [
            'data' => $order_item_ids
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'refund', $this->signAct($post_data));
    }

    /**
     * 删除过期账户接口
     * @param array $accounts 账户列表
     * @param int $type 账户类型 0 静态 1 固态
     */
    public function mulDeleteAccounts($accounts = [], $type = 0)
    {
        $post_data = [
            'accounts' => $accounts,
            'type' => $type
        ];
        
        return $this->client->request('post', $this->config ['server_url'] . 'mul-delete-accounts', $this->signAct($post_data));
    }
}